<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page language="java" import="dj.Constant"%>
<script type="text/javascript">
	function addCompany() {
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/app/user/manager/user_info.jsp',
			width : 300,
			height : 280,
			modal : true,
			title : '添加用户',
			buttons : [ {
				text : '添加',
				iconCls : 'icon-add',
				handler : function() {
					var d = $(this).closest('.window-body');
					$.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/userAction!admin_saveUser.action",
						dataType : "json",
						beforeSend : function() {
							var isValid = $("#manager-userinfo-form").form('validate');
							if(isValid) {
								$.messager.progress({
									text : "提交数据，请等待..."
								});
							}
							return isValid;
						},
						data : $("#manager-userinfo-form").serialize() + '&userType=<%=Constant.USERTYPE_COMPANY %>',
						error : function(info) {
							$.messager.progress('close');
							eval(info.responseText);
						},
						success : function(result) {
							$.messager.show({
								msg : result.msg,
								title : '提示'
							});
							$.messager.progress('close');
							$('#user-company-list-dg').datagrid('reload');
							d.dialog('destroy');
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}

	function editCompany(id) {
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/app/user/manager/user_info.jsp',
			width : 300,
			height : 280,
			modal : true,
			title : '修改用户',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');
					$.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/userAction!admin_setUserInfo.action",
						dataType : "json",
						beforeSend : function() {
							var isValid = $("#manager-userinfo-form").form('validate');
							if(isValid) {
								$.messager.progress({
									text : "提交数据，请等待..."
								});
							}
							return isValid;
						},
						data : $("#manager-userinfo-form").serialize() + "&id=" + id,
						error : function(info) {
							$.messager.progress('close');
							eval(info.responseText);
						},
						success : function(result) {
							$.messager.show({
								msg : result.msg,
								title : '提示'
							});
							$.messager.progress('close');
							$('#user-company-list-dg').datagrid('reload');
							d.dialog('destroy');
						}
					});
				}
			} ],
			onLoad : function() {
				$.ajax({
					type : "post",
					dataType : "json",
					url : "${pageContext.request.contextPath}/userAction!admin_getUserInfo.action",
					data : "id=" + id,
					error : function(info) {
						$.messager.show({
							msg : '获取信息失败！',
							title : '提示'
						});
						eval(info.responseText);
					},
					success : function(userInfo) {
						$("#name").attr("disabled", true);
						$("#name").val(userInfo.name);
						$("#displayName").val(userInfo.displayName);
						$("#email").val(userInfo.puserExtend.email);
						$("#addr").val(userInfo.puserExtend.addr);
						$("#phone").numberbox("setValue", userInfo.puserExtend.phone);
					}
				});
			},
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}
	
	function changeCompanyState(id, name, state) {
		var infoMsg = "";
		if('<%=Constant.STATE_OPEN%>' == state) {
			infoMsg = "您确定要解锁用户 “" + name + "” 吗？";
		} else if('<%=Constant.STATE_CLOSE %>' == state) {
			infoMsg = '您确定要锁定用户 “' + name + '” 吗？';
		} else {
			$.messager.alert('提示','非法的状态！');
			return;
		}
		$.messager.confirm('提示', infoMsg, function(r){
			if(r) {
				$.ajax({
					type : "post",
					dataType : "json",
					url : "${pageContext.request.contextPath}/userAction!admin_changeUserState.action",
					data : "id=" + id + "&userState=" + state,
					error : function(info) {
						$.messager.show({
							msg : '获取信息失败！',
							title : '提示'
						});
						eval(info.responseText);
					},
					success : function(result) {
						$.messager.show({
							msg : result.msg,
							title : '提示'
						});
						$.messager.progress('close');
						$('#user-company-list-dg').datagrid('reload');
					}
				});
			}
		});
	}
	
	//查询功能
	function searchCompanys() {
		$("#user-company-list-dg").datagrid("options").queryParams = getQueryParams("user-manager-company-search");
		autoLoad($("#user-company-list-dg").datagrid("getPager"), 1);
	}
	
	$(function() {
		$('#user-company-list-dg').datagrid({
			url : "${pageContext.request.contextPath }/userAction!admin_getAllCompanyUser.action",
			onLoadError : function(retValue) {
				eval(retValue.responseText);
			},
			singleSelect : true,
			pagination : true,
			fitColumns : true,
			pageSize : 10,
			pageList : [ 5, 10, 15, 20 ],
			nowrap : true,
			border : false,
			idField : 'id',
			loadFilter: function(data){  		
			    if(data && data.rows && data.rows.length > 0) {
			    	for(var x = 0; x < data.rows.length; x++) {
			    		data.rows[x].email = data.rows[x].puserExtend.email;
			    		data.rows[x].phone = data.rows[x].puserExtend.phone;
			    		data.rows[x].addr = data.rows[x].puserExtend.addr;
			    	}
			    }
			    return data;
			},
			toolbar : [ {
				text : '添加用户',
				iconCls : 'icon-add',
				handler : function() {
					addCompany();
				}
			}],
			columns : [ [ {
				field : 'name',
				title : '用户名',
				width : 150
			}, {
				field : 'displayName',
				title : '企业名称',
				width : 180,
				formatter : function(value) {
					if (value && 'null' != value) {
						return value;
					} else {
						return "";
					}
				}
			}, {
				field : 'email',
				title : '邮箱',
				width : 200
			}, {
				field : 'phone',
				title : '电话',
				width : 180
			}, {
				field : 'addr',
				title : '地址',
				width : 180
			}, {
				field : 'userState',
				title : '用户状态',
				width : 80,
				align : 'center',
				formatter : function(value) {
					if('<%=Constant.STATE_OPEN%>' == value) {
						return "正常";
					} else {
						return "锁定";
					}
				}
			}, {
				field : 'id',
				title : '操作',
				width : 80,
				align : 'center',
				formatter : function(value, rowData, rowIndex) {
					if('<%=Constant.STATE_OPEN%>' == rowData.userState) {
						return "<a href='javascript:void(0)' onclick='editCompany(\"" + value + "\")'><img src='${pageContext.request.contextPath }/images/pencil.png' border='0'></a>&nbsp;&nbsp;" + 
								"<a href='javascript:void(0)' onclick='changeCompanyState(\"" + value + "\", \"" + rowData.name + "\", \"<%=Constant.STATE_CLOSE%>\")'><img src='${pageContext.request.contextPath }/images/lock.png' border='0'></a>";
					} else {
						return "<a href='javascript:void(0)' onclick='editUser(\"" + value + "\")'><img src='${pageContext.request.contextPath }/images/pencil.png' border='0'></a>&nbsp;&nbsp;" + 
								"<a href='javascript:void(0)' onclick='changeCompanyState(\"" + value + "\", \"" + rowData.name + "\", \"<%=Constant.STATE_OPEN%>\")'><img src='${pageContext.request.contextPath }/images/lock_break.png' border='0'></a>";
					}
				}
			} ] ]
		});
	});
</script>
<div class="easyui-layout" data-options="fit:true">

	<div data-options="region:'north', title:'查询', iconCls: 'icon-search'" style="height:80px; padding: 0;">
		<div style="padding-top: 10px; padding-left: 20px">
			<form id="user-manager-company-search">
				<span>用户名：<input type="text" id="user-manager-username" name="name"></span>&nbsp;&nbsp;
				<span>企业名称：<input type="text" id="user-manager-displayName" name="displayName"></span>&nbsp;&nbsp;
				<span>电话号码：<input type="text" id="user-manager-phone" name="puserExtend.phone"></span>&nbsp;&nbsp;
				<span><a href="javascript:void(0)" class="easyui-linkbutton" onclick="searchCompanys()" data-options="iconCls:'icon-search'">查询</a></span>
			</form>
		</div>
	</div>

	<div data-options="region:'center', title:'企业账号管理', iconCls: 'icon-group'" style="padding: 0;">
		<table id="user-company-list-dg"></table>
	</div>

</div>
